package org.baiyun.project.cal.web;


import org.baiyun.project.cal.web.service.CalServiceImpl;
import org.baiyun.project.cal.web.service.ICalService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * ServletContext 作用于一个web应用程序
 * 每个程序只有一个ServletContext对象
 */

public class CalServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");

        int a = Integer.parseInt(req.getParameter("a"));
        int b = Integer.parseInt(req.getParameter("b"));
        char op = req.getParameter("op").charAt(0);

        ICalService calService = new CalServiceImpl();

        ServletContext sc = this.getServletContext();
        System.out.println(sc.getInitParameter("testparm"));

        resp.getWriter().write("计算结果是："+calService.doCompute(a,b,op));

    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

}
